<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Install MongoDig</title>
</head>
<body>
<?php 
if(isset($_POST['submit'])){
$db_host = $_POST['db_host'];
$db_port = $_POST['db_port'];
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_name = $_POST['db_name'];
$site_title = $_POST['site_title'];
$admin_email = $_POST['admin_email'];
$perpage = $_POST['perpage'];
$site_url= substr('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],0,-12);
$site_path = dirname(__FILE__);	
$string= <<<END
<?php
/**
 * MongoDig
 *
 * @copyright  Copyright (c) 2010 lanyanguang (http://www.lanyg.com)
 * @license    GNU General Public License 2.0
 * @version    \$Id\$
 */

/** 配置项数组 */
\$config = array();

/** 配置MongoDB */
\$config['db_host'] = '$db_host';
\$config['db_port'] = '$db_port';
\$config['db_username'] = '$db_username';
\$config['db_password'] = '$db_password';
\$config['db_name'] = '$db_name';

/** 配置站点 */
\$config['site_title'] = '$site_title';
\$config['site_url'] = '$site_url';
\$config['site_path'] = '$site_path';
\$config['admin_email'] = '$admin_email';
\$config['perpage'] = '$perpage';
\$config['mod_rewrite'] = '0';
\$config['template'] = 'default';
\$config['version'] = '1.0.0';

/** 配置Cookie */
\$config['cookie_time'] = '2592000';
\$config['cookie_name'] = '$site_title';
END;
$fp = fopen('config.php', 'w');
fwrite($fp, $string);
fclose($fp);

/** 连接数据库 */
require_once $site_path . '/config.php';
require_once $site_path . '/includes/functions/common.php';
$conn = connect();

/** 选择数据库 */
$db = $conn->selectDB($config['db_name']);
//插入数据库
$db->drop();
$db = $conn->selectDB($config['db_name']);
//cat
$collection = $db->cat;
$collection->ensureIndex(array('name'=>1),array('unique'=> true, 'dropDups'=>true,'background'=>true));
$collection->batchInsert(
array(
	 array(
	 	'name'=>'Entertainment'
	 ),
	  array(
	 	'name'=>'Gaming'
	 ),
	 	  array(
	 	'name'=>'Science'
	 ),
	 	  array(
	 	'name'=>'Sports'
	 ),
	 	  array(
	 	'name'=>'Technology'
	 ),
	 	  array(
	 	'name'=>'Videos'
	 ),
	 	  array(
	 	'name'=>'World & Business'
	 )
	) 
);
//user
$collection = $db->user;
$collection->ensureIndex(array('username'=>1),array('unique'=> true, 'dropDups'=>true,'background'=>true));
//vote
$collection = $db->vote;
$collection->ensureIndex(array('topic_id'=>1,'user_id'=>1),array('unique'=> true, 'dropDups'=>true,'background'=>true));
//topic
$collection = $db->topic;
$collection->ensureIndex(array('url'=>1),array('unique'=> true, 'dropDups'=>true,'background'=>true));
$collection->ensureIndex(array('comment.user_id'=>1),array('unique'=> true, 'dropDups'=>true,'background'=>true));
echo "<a href=\"$site_url\">$site_title</a>";
echo "<br/>";
echo "<a href=\"$site_url/admin\">Admin</a>";
exit;
}
?>
<div>
<h1>Install MongoDig</h1>
<form name="form1" method="post" action="install.php">
<h2>MongoDB:</h2>
<label>db_host</label> <input name="db_host" type="text" value="localhost" /><br/>
<label>db_port</label> <input name="db_port" type="text" value="27017" /><br/>
<label>db_username</label> <input name="db_username" type="text" value="" /><br/>
<label>db_password</label> <input name="db_password" type="text" value="" /><br/>
<label>db_name</label> <input name="db_name" type="text" value="mongodig" /><br/>
<br/><br/>
<h2>WebSite:</h2>
<label>site_title</label> <input name="site_title" type="text" value="MongoDig" /><br/>
<label>admin_email</label> <input name="admin_email" type="text" value="lan@noez.com" /><br/>
<label>perpage</label> <input name="perpage" type="text" value="10" /><br/>
<br/><br/>
<input type="submit" value="Install" name="submit" />
</form>
</div>
</body>
</html>